package com.tech.koufu.ui.view;

 
import com.tech.koufu.R;     
import android.os.Bundle;
import android.support.annotation.Nullable; 
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;  
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 大赛 fragment
 * 
 * @author jl_luo
 * 
 */
public class CompetitionContentFragment extends BaseContainerFragment implements OnClickListener{
private static final String TAG = CompetitionContentFragment.class.getName();
	
	private View rootView;
//	private PullToRefreshListView mPullToRefreshListView;
//	private ListView actualListView;
//	private CompetitionAdapter mAdapter;
	
	private boolean showLeft = true;
	private TextView txtAllCompetition,txtMyCompetition;
	private View allTabBaseLine,myTabBaseLine;
	CompetitinListView allCompetition,myCompetiton;
	FragmentTransaction transaction;
	FrameLayout allLayout,myLayout;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
		if (rootView == null) {Log.d(TAG, "~~~~~CompetitionContentFragment~onCreateView~~~~~~~~~~");
			rootView = inflater.inflate(R.layout.fragment_competition, container, false);
			initView();
//			initData();
		}else{
			Log.d(TAG, "~~~no~~CompetitionContentFragment~onCreateView~~~~~~~~~~");
		}
		ViewGroup parent = (ViewGroup) rootView.getParent();
		if (parent != null) {
			parent.removeView(rootView);
		}
	
		return rootView; 
	}

	private void initView() {  
		rootView.findViewById(R.id.rl_tab_title_all_competition).setOnClickListener(this);
		rootView.findViewById(R.id.rl_tab_title_my_competition).setOnClickListener(this);
		txtAllCompetition = (TextView)rootView.findViewById(R.id.tab_title_all_competition);
		txtMyCompetition = (TextView)rootView.findViewById(R.id.tab_title_my_competition);
		allTabBaseLine = (View)rootView.findViewById(R.id.tab_title_all_competition_baseview);
		myTabBaseLine = (View)rootView.findViewById(R.id.tab_title_my_competition_baseview); 
		allLayout = (FrameLayout)rootView.findViewById(R.id.container_competition);
		myLayout = (FrameLayout)rootView.findViewById(R.id.container_mycompetition);
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState); 
		initData();
	}

	private void initData() {  
		if(allCompetition ==null){ 
			allCompetition = CompetitinListView.newInstance(1); 
		}
//		if(myCompetiton ==null){ 
//			myCompetiton = CompetitinListView.newInstance(2);
//		}
//		transaction = getChildFragmentManager().beginTransaction();
//		transaction.add(R.id.container_competition, allCompetition);
//		transaction.add(R.id.container_competition, myCompetiton);
//		transaction.show(allCompetition).hide(myCompetiton); 
//		transaction.commitAllowingStateLoss();
		replaceFragment(R.id.container_competition, allCompetition, false);
	}
	public void mainManualRefresh(int tag){
		if(allTabBaseLine.getVisibility()==View.VISIBLE){
				allCompetition.manualRefresh(tag);
		}else if(myTabBaseLine.getVisibility()==View.VISIBLE){
			if(myCompetiton!=null){
				myCompetiton.manualRefresh(tag);
			}
		}
	}
	public void mainTopRefresh(){
		allCompetition.isload = false;
		if(myCompetiton!=null){
			myCompetiton.isload = false;
		}
		mainManualRefresh(1);
	}
	@Override
	public void onClick(View v) { 
		switch (v.getId()) {
		case R.id.rl_tab_title_all_competition:
			if(allTabBaseLine.getVisibility()!=View.VISIBLE){
				setColor(true);
				if(allCompetition ==null){
					Log.d(TAG, "~~~~~~allCompetition~~~onCreateView~~~~~~~");
					allCompetition = CompetitinListView.newInstance(1);
				}Log.d(TAG, "~~~~~~allCompetition~~~onCreateView~~~1~~~~");
//				replaceFragment(R.id.container_competition, allCompetition, true); 
				myLayout.setVisibility(View.GONE);
				allLayout.setVisibility(View.VISIBLE);
			}
			break;
		case R.id.rl_tab_title_my_competition:
			if(myTabBaseLine.getVisibility()!=View.VISIBLE){
				setColor(false);
				if(myCompetiton ==null){
					Log.d(TAG, "~~~~~~myCompetiton~~~onCreateView~~~~~~~");
					myCompetiton = CompetitinListView.newInstance(2); 
					replaceFragment(R.id.container_mycompetition, myCompetiton, true);
				} 
				
				myLayout.setVisibility(View.VISIBLE);
				allLayout.setVisibility(View.GONE);
				Log.d(TAG, "~~~~~~myCompetiton~~~onCreateView~~~1~~~~");
//				replaceFragment(R.id.container_competition, myCompetiton, true);
				
				myCompetiton.manualRefresh(0);
			 
//				transaction = getChildFragmentManager().beginTransaction(); 
//				transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//				transaction.show(myCompetiton).hide(allCompetition); 
//				transaction.commit();
			}
			break;

		default:
			break;
		}
	}
	
	private void setColor(boolean isleft){
		if(isleft){
			txtAllCompetition.setTextColor(getResources().getColor(R.color.kfColorRed));
			allTabBaseLine.setVisibility(View.VISIBLE);
			txtMyCompetition.setTextColor(getResources().getColor(R.color.textColorGray_888888));
			myTabBaseLine.setVisibility(View.GONE);
		}else{
			txtAllCompetition.setTextColor(getResources().getColor(R.color.textColorGray_888888));
			allTabBaseLine.setVisibility(View.GONE);
			txtMyCompetition.setTextColor(getResources().getColor(R.color.kfColorRed));
			myTabBaseLine.setVisibility(View.VISIBLE);
		}
	}
 
	 
	 
}
